package com.wkhyapp.lm.weigit;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Layout;
import android.text.TextPaint;
import android.text.TextWatcher;
import android.text.method.ArrowKeyMovementMethod;
import android.text.method.MovementMethod;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.TextView;

/**
 * Created by fang on 2021/10/30.
 */

public class EditTextView extends AppCompatTextView implements TextWatcher {

    private final int INPUT_BOX_NUM = 6;

    private int mBoxSpace = 10;

    public EditTextView(Context context) {
        this(context,null);
    }

    public EditTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public EditTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setFocusable(true);
        setFocusableInTouchMode(true); //支持触摸聚焦
        setClickable(true);
        setGravity(Gravity.CENTER_VERTICAL);
        setMaxLines(1);
        setFilters(new InputFilter[]{
                new InputFilter.LengthFilter(INPUT_BOX_NUM)
        });
        if(Build.VERSION.SDK_INT>=23) {
            setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE);
        }
        mBoxSpace = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10,context.getResources().getDisplayMetrics());
        addTextChangedListener(this);

    }

    @Override
    protected boolean getDefaultEditable() {
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // super.onDraw(canvas);
        TextPaint paint = getPaint();

        int strokeWidth = (int) paint.getStrokeWidth();


        int boxWidth  =  (getWidth()- strokeWidth*2 - (INPUT_BOX_NUM-1)*mBoxSpace)/INPUT_BOX_NUM;
        int boxHeight = getHeight() -  strokeWidth*2;

        RectF rectF = null;

        Paint.Style style = paint.getStyle();
        Paint.Align align = paint.getTextAlign();
        paint.setTextAlign(Paint.Align.CENTER);

        paint.setStyle(Paint.Style.STROKE);
        paint.setTextAlign(Paint.Align.CENTER);
        String text = getText().toString();
        for (int i=0;i<INPUT_BOX_NUM;i++){
            rectF = new RectF(
                    i*(boxWidth+mBoxSpace),
                    0,
                    i*(boxWidth+mBoxSpace)+boxWidth,
                    boxHeight
            );

            Paint.FontMetrics fontMetrics = paint.getFontMetrics();
            float top = fontMetrics.top;//为基线到字体上边框的距离,即上图中的top
            float bottom = fontMetrics.bottom;//为基线到字体下边框的距离,即上图中的bottom
            int baseLineY = (int) (rectF.centerY() - top/2 - bottom/2);//基线中间点的y轴计算公式

            canvas.drawRoundRect(rectF,0,0,paint);
            if(text.length()>i) {
                String CH = String.valueOf(text.charAt(i));
                canvas.drawText(CH,rectF.centerX(),baseLineY,paint);
            }

        }
        paint.setStyle(style);
        paint.setTextAlign(align);
    }

    /**
     * 控制是否返回完整文本
     * @return
     */
    @Override
    public boolean getFreezesText() {
        return true;
    }

    @Override
    public Editable getText() {
        return (Editable) super.getText();
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, BufferType.EDITABLE);
    }
    /**
     * 控制光标展示
     * @return
     */
    @Override
    protected MovementMethod getDefaultMovementMethod() {
        return ArrowKeyMovementMethod.getInstance();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}